package com.abl.common.util.coll;

import com.alibaba.fastjson2.JSONObject;

import java.util.*;

public class MapKit {
    /**
     * 根据keys，取出并插入到新的HashMap
     *
     * @param map 内容
     * @return keys Arrays.asList("sort", "status", "desc", "name", "role_key")
     */
    public static HashMap set(Map map, Collection<?> keys) {
        var hashMap = new HashMap<>();
        keys.forEach(key -> hashMap.put(key, map.get(key)));
        return hashMap;
    }

    public static HashMap set(Map map, String... keys) {
        return set(map, Arrays.asList(keys));
    }

    public static HashMap set(JSONObject jsonObject, Collection<String> keys) {
        var hashMap = new HashMap<>();
        keys.forEach(key -> hashMap.put(key, jsonObject.get(key)));
        return hashMap;
    }

    public static HashMap set(JSONObject jsonObject, String... keys) {
        return set(jsonObject, Arrays.asList(keys));
    }

    /**
     * 取出指定对象，插入到新的HashMap
     *
     * @param map        内容
     * @param selectKeys 查询key
     * @param insertKeys 插入key
     * @return 返回新HashMap
     */
    public static HashMap set(Map<String, Object> map, List<String> selectKeys, List<String> insertKeys) {
        var hashMap = new HashMap<>();
        if (selectKeys.size() != insertKeys.size()) {
            throw new RuntimeException("selectKeys 与 insertKeys 长度不同，无法执行替换逻辑");
        }
        for (int i = 0; i < selectKeys.size(); i++) {
            var selectKey = selectKeys.get(i);
            var insertKey = insertKeys.get(i);
            hashMap.put(insertKey, map.get(selectKey));
        }
        return hashMap;
    }

}
